package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.Feidan8Service;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwThirdback;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwThirdbackService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 飞单宝渠道
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class Feidan8ServiceImpl implements Feidan8Service {

    Logger logger = LoggerFactory.getLogger(Feidan8ServiceImpl.class);
    @Autowired
    KwThirdbackService kwThirdbackService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(User user, UserInfo userInfo) throws Exception {

        String url = ConfigUtil.getConfig("feidan8.url");
        String token = ConfigUtil.getConfig("feidan8.token");
        url = url + "?token=" + token;

        JSONObject userJson = new JSONObject();
        userJson.put("sub_source", "短信名单");
        userJson.put("district_id", userInfo.getAreaCode());
        userJson.put("name", user.getName());
        userJson.put("phone", user.getMobile());//手机号
        userJson.put("sex", user.getSex() == 0 ? "M" : "F");
        userJson.put("birth", DateUtils.dateToStr(userInfo.getBirthday(), "yyyy-MM-dd"));//城市
        userJson.put("req_time", DateUtils.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        String ip = userInfo.getIp();
        if (StringUtils.isBlank(ip)) {
            ip = "127.0.0.1";
        }
        userJson.put("client_ip", ip);
        JSONObject customJson = new JSONObject();
        customJson.put("期望贷款", userInfo.getExpectAmount().intValue());
        switch (userInfo.getPolicy()) {
            case 1:
                customJson.put("寿险保单", "无");
                break;
            case 2:
                customJson.put("寿险保单", "保单额度2400以下");
                break;
            case 3:
                customJson.put("寿险保单", "保单额度2400以上");
                break;

        }
        switch (userInfo.getCredit()) {
            case 1:
                customJson.put("信用情况", "无信用卡无贷款");
                break;
            case 2:
                customJson.put("信用情况", "有信用卡或贷款");
                break;

        }
        switch (userInfo.getHouse()) {
            case 1:
                customJson.put("名下房产", "无房");
                break;
            case 2:
                customJson.put("名下房产", "有房贷");
                break;
            case 3:
                customJson.put("名下房产", "有房无贷");
                break;

        }
        switch (userInfo.getCar()) {
            case 1:
                customJson.put("名下汽车", "无车");
                break;
            case 2:
                customJson.put("名下汽车", "有车贷");
                break;
            case 3:
                customJson.put("名下汽车", "有车无贷");
                break;

        }
        switch (userInfo.getFund()) {
            case 0:
                customJson.put("公积金", "无公积金");
                break;
            case 1:
                customJson.put("公积金", "1年以内");
                break;
            case 2:
                customJson.put("公积金", "1年以上");
                break;

        }
        switch (userInfo.getSocialSecurity()) {
            case 0:
                customJson.put("社会保险", "无社保");
                break;
            case 1:
                customJson.put("社会保险", "1年以内");
                break;
            case 2:
                customJson.put("社会保险", "1年以上");
                break;

        }
        switch (userInfo.getOccupation()) {
            case 0:
                customJson.put("您的职业", "白领");
                break;
            case 1:
                customJson.put("您的职业", "私企业主");
                break;
            case 2:
                customJson.put("您的职业", "公务员");
                break;

        }
        switch (userInfo.getSalaryMethod()) {
            case 1:
                customJson.put("工资发放", "银行转账");
                break;
            case 2:
                customJson.put("工资发放", "现金发放");
                break;

        }
        switch (userInfo.getWorkDuration()) {
            case 6:
                if (userInfo.getOccupation() != 1) {
                    customJson.put("工作时间", "6个月内");
                } else {
                    customJson.put("营业执照", "6个月内");
                }
                break;
            case 12:
                if (userInfo.getOccupation() != 1) {
                    customJson.put("工作时间", "12个月内");
                } else {
                    customJson.put("营业执照", "12个月内");
                }
                break;
            case 24:
                if (userInfo.getOccupation() != 1) {
                    customJson.put("工作时间", "1年以上");
                } else {
                    customJson.put("营业执照", "1年以上");
                }
                break;

        }
        switch (userInfo.getSalary()) {
            case 1:
                customJson.put("月收入", "3000以下");
                break;
            case 2:
                customJson.put("月收入", "3001-5000");
                break;
            case 3:
                customJson.put("月收入", "5001-8000");
                break;
            case 4:
                customJson.put("月收入", "8001-10000");
                break;
            case 5:
                customJson.put("月收入", "1万以上");
                break;

        }
        switch (userInfo.getWxloan()) {
            case 1:
                customJson.put("微粒贷", "是");
                break;
            case 2:
                customJson.put("微粒贷", "否");
                break;
        }
        userJson.put("custom", customJson);

        //接口调用信息表
        KwThirdback thirdback = new KwThirdback();
        thirdback.setInterfaceCode("feidan8");
        thirdback.setInterfaceName("飞单宝");
        thirdback.setMobile(user.getMobile());
        thirdback.setSendUrl(url.toString());
        thirdback.setParams(userJson.toString());
        thirdback.setCode(Constants.BUSINESS_STATUS_FAIL);

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, userJson.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(200);
                thirdback.setHttpContent(hr.toString());

                logger.info("==Feidan8ServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("status");
                String rMsg = returnMsg.getString("msg");
                if ("1".equals(rCode)) {
                    thirdback.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    thirdback.setMessage("成功");
                    logger.info("==Feidan8ServiceImpl.applyLoan business success");
                } else {
                    thirdback.setCode(Constants.BUSINESS_STATUS_OTHER);
                    thirdback.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==Feidan8ServiceImpl.applyLoan business fail");
                }
            } else {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(code);
                thirdback.setHttpContent(hr.toString());
                logger.warn("==Feidan8ServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            thirdback.setHttpCode(408);
            thirdback.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwThirdbackService.insert(thirdback);
            return thirdback.getCode();
        }
    }

}
